﻿using System;
using System.ComponentModel;
using ArtMath.Parameter;
using ArtMath.Resource;
using ArtMath.Core.Helper;

namespace ArtMath.Data
{
    /// <summary>
    /// 适用于画板的带值表达式
    /// </summary>
    [Serializable]
    public class PadStringX : DocStringX
    {
        [Browsable(false)]
        public SketchPad LinkedPad { get; }
        public PadStringX(SketchPad pad) : base(pad.Document)
        {
            LinkedPad = pad;
        }
        public PadStringX(SketchPad pad, string expression) : this(pad)
        {
            SetExpression(expression, false);
        }
        public PadStringX(SketchPad pad, double value) : this(pad)
        {
            SetExpression(value, false);
        }
        protected override void valueChanged(BasicParameter sender, ValueChangedEventArgs e)
        {
            base.valueChanged(sender, e);
            LinkedPad.Canvas.Invalidate();
        }
        public override void SetExpression(double value, bool raiseEvent = true)
        {
            base.SetExpression(value, raiseEvent);
            if (raiseEvent)
            {
                LinkedPad.Canvas.Invalidate();
            }
        }
        public override void SetExpression(string value, bool raiseEvent = true, bool showMsg = true)
        {
            base.SetExpression(value, raiseEvent, showMsg);
            if (!string.IsNullOrEmpty(value) && raiseEvent)
            {
                LinkedPad.Canvas.Invalidate();
            }
        }
        protected override bool setString(string origin, string std, bool showMsg)
        {
            if (base.setString(origin, std, showMsg))
            {
                return true;
            }
            else
            {
                if (showMsg)
                {
                    LinkedPad.ShowMessage(string.Format(Resources.Translate("InvalidExpression"), origin), MessageType.Error);
                }
                return false;
            }
        }
    }
}
